<?php
/**
 * Created by PhpStorm.
 * User: sunshine
 * Date: 17-4-18
 * Time: 下午5:01
 */

namespace Admin\Controller;


use Think\Controller;

class UserController extends Controller
{
    public function login()
    {
        $resultArr = array(
            'registerUrl' => U('Admin/User/register'),
            'warningStyle' => 'display: none;'
        );
        $userInfo = GetUserInfo();
        if ($userInfo['status'] == false) {
            $postInfo = I('post.');
            if ($postInfo) {
                if ($postInfo['username'] != '' && $postInfo['password'] != '' && $postInfo['geetest_challenge'] != '' && $postInfo['geetest_validate'] != '' && $postInfo['geetest_seccode'] != '') {
                    $verifyResult = getVerify(1, 2);
                    if ($verifyResult) {
                        $usermodel = new \Admin\Model\UserModel();
                        $result = $usermodel->login($postInfo['username'], $postInfo['password'], $_SERVER['REMOTE_ADDR']);
                        if ($result['status']) {
                            cookie('ids', $result['cookie']);
                            header('Location: ' . U('Home/Index/index'));
                        } else if ($result['errcode'] == 103) {
                            $registerUrl = U('Admin/User/register');
                            $resultArr = array(
                                'loginUrl' => $registerUrl,
                                'warningStyle' => 'opacity: 1;',
                                'warningInfo' => '密码错误,请重试'
                            );
                            $this->assign('result', $resultArr);
                            $this->display();
                        } else if ($result['errcode'] == 106) {
                            $registerUrl = U('Admin/User/register');
                            $resultArr = array(
                                'loginUrl' => $registerUrl,
                                'warningStyle' => 'opacity: 1;',
                                'warningInfo' => '服务器故障,请稍后重试'
                            );
                            $this->assign('result', $resultArr);
                            $this->display();
                        }else if ($result['errcode'] == 101) {
                            $registerUrl = U('Admin/User/register');
                            $resultArr = array(
                                'loginUrl' => $registerUrl,
                                'warningStyle' => 'opacity: 1;',
                                'warningInfo' => '用户不存在'
                            );
                            $this->assign('result', $resultArr);
                            $this->display();
                        }
                    } else {
                        $resultArr = array(
                            'registerUrl' => U('Admin/User/register'),
                            'warningStyle' => 'opacity: 1;',
                            'warningInfo' => '非法数据'
                        );
                        $this->assign('result', $resultArr);
                        $this->display();
                    }
                } else {
                    $this->assign('result', $resultArr);
                    $this->display();
                }
            }else{
                $this->assign('result', $resultArr);
                $this->display();
            }

        }else{
            header('Location: '.U('Home/Index/index'));
        }
    }

    public function logout()
    {
        $userInfo = GetUserInfo();
        if($userInfo['status'] == true){
            $usermodel = new \Admin\Model\UserModel();
            $result = $usermodel->logout(cookie('ids'));
            if ($result){
                cookie('ids',null);
            }
        }
        header('Location: '.U('Home/Index/index'));
    }

    public function register()
    {
        $userInfo = GetUserInfo();
        $postInfo = I('post.');
        if ($postInfo) {//是否有post数据
            if ($postInfo['username'] != '' && $postInfo['password'] != '' && $postInfo['geetest_challenge'] != '' && $postInfo['geetest_validate'] != '' && $postInfo['geetest_seccode'] != '') {
                $verifyResult = getVerify(1, 2);
                if ($verifyResult) {
                    $usermodel = new \Admin\Model\UserModel();
                    $result = $usermodel->addUser($postInfo['username'], $postInfo['password']);
                    if ($result['status']) {
                        header('Location: ' . U('Home/Index/index'));
                    } else {
                        $loginUrl = U('Admin/User/login');
                        $resultArr = array(
                            'loginUrl' => $loginUrl,
                            'warningStyle' => 'opacity: 1;',
                            'warningInfo' => '注册失败,可能已存在,请重试!'
                        );
                        $this->assign('result', $resultArr);
                        $this->display();
                    }
                } else {
                    $loginUrl = U('Admin/User/login');
                    $resultArr = array(
                        'loginUrl' => $loginUrl,
                        'warningStyle' => 'opacity: 1;',
                        'warningInfo' => '极验证验证失败' . (string)$verifyResult
                    );
                    $this->assign('result', $resultArr);
                    $this->display();
                }

            } else {
                $resultArr = array(
                    'loginUrl' => U('Admin/User/login'),
                    'warningStyle' => 'opacity: 1;',
                    'warningInfo' => '非法数据'
                );
                $this->assign('result', $resultArr);
                $this->display();
            }
        } else {
            if ($userInfo['status']) {//是否登录
                header('Location: ' . U('Home/Index/index'));
            } else {
                $loginUrl = U('Admin/User/login');
                $resultArr = array(
                    'loginUrl' => $loginUrl,
                    'warningStyle' => 'display: none;'
                );
                $this->assign('result', $resultArr);
                $this->display();
            }
        }


    }

    public function ajaxIfHadUserName()
    {
        $returnArr = array('status' => false);
        $userName = I('post.userName');
        if ($userName) {
            $usermodel = new \Admin\Model\UserModel();
            $info = $usermodel->getUserInfoByUserName($userName, 0);
            $returnArr = array(
                'status' => $info['status']
            );
        }

        echo json_encode($returnArr);
    }

    public function ajaxGetVerify()
    {
        echo getVerify(1, 1);
    }
}